home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / snip9611.zip / QUERY.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  1KB  |  54 lines

  1. /* +++Date last modified: 02-Nov-1995 */
  2.  
  3. /*
  4. **  QUERY.C - Timed query with default for batch files
  5. **
  6. **  public domain by Bob Stout
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include <ctype.h>
  12. #include <stdlib.h>
  13. #include <conio.h>
  14.  
  15. main(int argc, char *argv[])
  16. {
  17.       int ch = '\0', def_ch = '\0';
  18.       char *prompt = "(y/n) ";
  19.       clock_t start, limit = (clock_t)0;
  20.  
  21.       if (1 < argc)
  22.       {
  23.             def_ch = toupper(*argv[1]);
  24.             if ('Y' == def_ch)
  25.                   prompt[1] = (char)def_ch;
  26.             else if ('N' == def_ch)
  27.                   prompt[3] = (char)def_ch;
  28.             else  def_ch = '\0';
  29.       }
  30.       fputs(prompt, stderr);
  31.       if (2 < argc)
  32.       {
  33.             start = clock();
  34.             limit = (clock_t)(CLK_TCK * atoi(argv[2]));
  35.       }
  36.       while ('Y' != ch && 'N' != ch)
  37.       {
  38.             while (!kbhit())
  39.             {
  40.                   if (limit && (limit <= (clock() - start)))
  41.                   {
  42.                         ch = def_ch;
  43.                         goto BYE;
  44.                   }
  45.             }
  46.             ch = toupper(getch());
  47.             if ('Y' != ch && 'N' != ch && (1 < argc))
  48.                   ch = def_ch;
  49.       };
  50. BYE:  fputc(ch, stderr);
  51.       fputc('\n', stderr);
  52.       return ('Y' == ch);
  53. }
  54.